package com.vn.rainbow.exoalarm.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.util.TypedValue;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.vn.rainbow.exoalarm.R;

public class BaseActivity extends ActionBarActivity {
	protected Typeface mFontStyle = null;
	protected ActionBar mActionBar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// mFontStyle = Util.getFontStyle(this, Constants.FONT_DEFAULT);
		initActionBar();
		// BaseApplication.analytics(getApplication(), getClass().getName());
	}

	@TargetApi(Build.VERSION_CODES.DONUT)
	public void setStatusBarColor(View statusBar, int color) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Window w = getWindow();
			w.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// status bar height
			int actionBarHeight = getActionBarHeight();
			int statusBarHeight = getStatusBarHeight();
			// action bar height
			statusBar.getLayoutParams().height = actionBarHeight
					+ statusBarHeight;
			statusBar.setBackgroundColor(color);
		}
	}

	public int getStatusBarHeight() {
		int result = 0;
		int resourceId = getResources().getIdentifier("status_bar_height",
				"dimen", "android");
		if (resourceId > 0) {
			result = getResources().getDimensionPixelSize(resourceId);
		}
		return result;
	}

	public int getActionBarHeight() {
		int actionBarHeight = 0;
		TypedValue tv = new TypedValue();
		if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
			actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
					getResources().getDisplayMetrics());
		}
		return actionBarHeight;
	}

	protected void initActionBar() {
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayHomeAsUpEnabled(true);
		mActionBar.setHomeButtonEnabled(true);
	}

	@Override
	public void setTitle(CharSequence title) {
		SpannableString s = new SpannableString(title);
		s.setSpan(null, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mActionBar.setTitle(s);
	}

	@Override
	public void setTitle(int titleId) {
		// TODO Auto-generated method stub
		setTitle(getResources().getString(titleId));
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		// if (!BaseApplication.isScreenLarge()) {
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// }
	}

	public Typeface getFontStyle() {
		return mFontStyle;
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			super.onBackPressed();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onStop() {
		super.onStop();
		// finish();
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	@Override
	public void onBackPressed() {
		super.onBackPressed();
		overridePendingTransition(R.anim.anim_back_left, R.anim.anim_back_right);
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	@Override
	public void finish() {
		super.finish();
		overridePendingTransition(R.anim.anim_back_left, R.anim.anim_back_right);
	}

	@TargetApi(Build.VERSION_CODES.ECLAIR)
	@Override
	public void startActivity(Intent intent) {
		// TODO Auto-generated method stub
		super.startActivity(intent);
		overridePendingTransition(R.anim.anim_right, R.anim.anim_left);
	}

	@Override
	protected void onDestroy() {
		Runtime.getRuntime().freeMemory();
		Runtime r = Runtime.getRuntime();
		r.gc();
		r.gc();
		super.onDestroy();
	}
}
